package com.bilibili.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilibili.entity.CommonResult;
import com.bilibili.entity.Follow;
import com.bilibili.entity.User;
import com.bilibili.service.UserService;
import com.bilibili.service.impl.UserServiceImpl;

@SuppressWarnings("serial")//抑制serialVersionUID字段警告
@WebServlet("/FollowIsMutual")
public class QueryFollowIsMutualServlet extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json; charset=utf-8");
		String follower_id = request.getParameter("follower_id");
		String followee_id = request.getParameter("followee_id");
		int followerId = 0,followeeId = 0;
		if(follower_id.equals("-1")) {
			followerId = ((User) request.getSession().getAttribute("loginUser")).getUserId();
			try {
				followeeId = Integer.parseInt(followee_id);
			}catch (Exception e) {
				// TODO: handle exception
			}
		}else {
			followeeId = ((User) request.getSession().getAttribute("loginUser")).getUserId();
			try {
				followerId = Integer.parseInt(follower_id);
			}catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		
		Follow follow = new Follow();
		follow.setFollowerId(followerId);
		follow.setFolloweeId(followeeId);
		UserService userService = new UserServiceImpl();
		if(userService.queryFollowIsMutual(follow) != null) {
			CommonResult cr = new CommonResult();
			cr.setCode(200);
			response.getWriter().write(cr.toJSONString());
		}else {
			CommonResult cr = new CommonResult();
			cr.setCode(409);
			response.getWriter().write(cr.toJSONString());
		}
	}
}
